const router = require('koa-router')();

//查
router.get('/api/list',async ctx => {
    //分页  pagenum 页码  limit 每页的条数
    try {

        let total = await ctx.mysql.query('select count(*) from userlist');
        console.log(total)

        let {pagenum=1,limit=3} = ctx.query;

        let startIndex = (pagenum-1)*limit;

        /**
         * 1   0   
         * 2   2
         * 3   4
         * */ 

        let data = await ctx.mysql.query(`select * from userlist limit ${startIndex},${limit}`);
        ctx.body = {
            code:1,
            data,
            total:total[0]['count(*)']
        }
    }catch(e){
        ctx.body = {
            code:0,
            msg:e
        }
    }  
})

//增
router.post('/api/add',async ctx => {
    let {username,password,address,idcard,phone,sex} = ctx.request.body;

    if(username && password && idcard){  //容错处理
        //1.查询此人是否存在？
        let user = await ctx.mysql.query('select * from userlist where idcard=?',[idcard]);

        if(user.length){
            //1.存在
            ctx.body = {
                code:0,
                msg:'此人已存在'
            }
        }else{
            //2.不存在
            try{
                let create_time = new Date();
                let data = await ctx.mysql.query('insert into userlist (username,password,address,idcard,phone,sex,create_time) values (?,?,?,?,?,?,?)',[username,password,address,idcard,phone,sex,create_time]);
                ctx.body = {
                    code:1,
                    msg:'添加成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数缺失'
        }
    }
   
})

//删除

router.get('/api/del',async ctx => {
    let {id} = ctx.query;
    if(id || id === 0){
        try{
            await ctx.mysql.query('delete from userlist where id=?',[id])
            ctx.body = {
                code:1,
                msg:'删除成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数缺失'
        }
    }
})

//修改  

router.post('/api/edit',async ctx => {
    let {username,password,address,idcard,phone,sex,id} = ctx.request.body;

    if(id && username && password && idcard){
        try{
            let create_time = new Date();
            await ctx.mysql.query('update userlist set username=?,password=?,address=?,idcard=?,phone=?,sex=?,create_time=? where id=?',[username,password,address,idcard,phone,sex,create_time,id])
            ctx.body = {
                code:1,
                msg:'修改成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数缺失'
        }
    }
})


module.exports = router
